package com.example.min.restappmobile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Alexander on 28.03.2015.
 */
@SuppressWarnings("ALL")
public class LoginActivity extends Activity {

    private static final String EMPTY_S = "";
    private EditText txtLogin;
    private EditText txtPw;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);

        txtLogin = (EditText) findViewById(R.id.txtLogin);
        txtPw = (EditText) findViewById(R.id.txtPassword);
        btnLogin = (Button) findViewById(R.id.btnLogin);
    }

    public void logIn(View view) {
        final String login = txtLogin.getText().toString();
        final String password = txtPw.getText().toString();
        if(login.equals(EMPTY_S) || password.equals(EMPTY_S)) {
            Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.login_required) , Toast.LENGTH_SHORT);
            toast.show();
            return;
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                String serverUrl = getString(R.string.api_server);
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(serverUrl + "/api/AccountApi");//?userLogin=" + txtLogin.getText().toString());
                List<NameValuePair> parameters = new ArrayList<NameValuePair>(2);
                parameters.add(new BasicNameValuePair("login", login));
                parameters.add(new BasicNameValuePair("password", password));

                HttpResponse response;
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btnLogin.setText("Запрос данных");
                        }
                    });
                    httpPost.setEntity(new UrlEncodedFormEntity(parameters));
                    response = httpClient.execute(httpPost);
                    String responseString = new BasicResponseHandler().handleResponse(response);
                    JSONObject account = new JSONObject(responseString);
                    //String successString = account.getString("success");
                    if (account.has("success") && !account.isNull("success")) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Session.setSessionItem("login", login);
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "Неверный логин или пароль", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                } catch (Exception ex) {
                    Toast.makeText(getApplicationContext(), getString(R.string.bad_request), Toast.LENGTH_SHORT).show();
                } finally {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btnLogin.setText("Войти");
                        }
                    });
                }
            }
        }).start();

        String str = "string";
    }
}
